<?php

/**
 * Midia form.
 *
 * @package    form
 * @subpackage Midia
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class MidiaForm extends PluginMidiaForm
{
	public function configure()
	{
		parent::configure();
		unset($this['id_midia'], $this['id_item'], $this['id_usuario'], $this['id_pagina'], $this['id_galeria'], $this['id_evento']);
		 
		$this->widgetSchema['nm_midia']     = new sfWidgetFormInputFile(
			array(),
			array('class' => 'required')
		);
		$this->widgetSchema['ds_midia']     = new sfWidgetFormInput();
		$this->validatorSchema['nm_midia']  = new MidiaValidatorFile(array('path' => sfConfig::get('sf_upload_dir'), 'required' => false, 'mime_types' => 'web_images'));
		
		// fazendo a tipagem da midia
		$this->widgetSchema['id_tipo'] = new sfWidgetFormInputHidden();
		
		$this->widgetSchema->setLabels(array(
        	'nm_midia'  => 'Arquivo',
            'ds_midia'  => 'Legenda',
            'tipo'		=> 'tipo'
        ));

        $this->validatorSchema->setOption('allow_extra_fields', true);
        $this->widgetSchema->setNameFormat('midia[%s]');
	}

	/*Sobreescrita do método doSave, para PODER salvar thumbnail =/ */
	protected function doSave ( $con = null )
	{
		//remove o thumb anterior
		$thumb_pre   = sfConfig::get('sf_thumbnail_dir').'/'.$this->getObject()->getNmMidia();
		if(file_exists($thumb_pre)) unlink(sfConfig::get('sf_thumbnail_dir').'/'.$this->getObject()->getNmMidia());

		//salvando a imagem. O Objeto é entao atualizado
		parent::doSave();
		
		//Salvando o thumbnail
		$midia   = new Midia();
		$midia->createThumb($this);
	}
}